// import store from './store'
/**
 * 建立websocket连接
 */

import store from './store'
import Vue from 'vue'
// 备用ip
export function websocket() {
  const ws = new WebSocket('ws://112.124.67.119:8080/test')
  ws.onopen = function(e) {
    console.log('链接打开')
    var messageA = {
      quote_head: {
        id: '1000',
        version: '1',
        type: '11',
        exchange: '23123',
        symbol: '300020',
        d1: '0',
        d2: '0',
        t1: '0',
        t2: '0',
        rec_count: '1',
        compress: '0',
        token: 'AFwersdfadsfsdfpoiuhsfwersdfadsfsdfpoixx'
      },
      subscribe: store.state.arrexchange.join(',')
      // subscribe: '23123300117'
    }
    var messageJsonA = JSON.stringify(messageA)
    ws.send(messageJsonA)
  }
  ws.onmessage = function(e) {
    console.log(e.data)
    var obj1 = JSON.parse(e.data)
    // for (let i = 0; i < obj1.report_data.length; i++) {
    // console.log(...obj1.report_data)
    store.state.dataHQ = []
    store.state.dataHQ.push(...obj1.report_data)
    console.log(dataHQ)
    // console.log(store.state.dataHQ)
    for (let i = 0; i < store.state.dataHQ.length; i++) {
      for (let y = 0; y < store.state.data1.length; y++) {
        if (
          store.state.data1[y].transaction.symbol ===
          store.state.dataHQ[i].symbol
        ) {
          store.state.data1[y].transaction.exchange = store.state.dataHQ[i].name
          store.state.data1[y].transaction.open_order.executed_no =
            store.state.dataHQ[i].new_price
          Vue.set(store.state.data1, y, store.state.data1[y])
        } else if (
          store.state.transactiondatas[store.state.INDEX][y].transaction
            .symbol === store.state.dataHQ[i].symbol
        ) {
          store.state.transactiondatas[store.state.INDEX][
            y
          ].transaction.exchange = store.state.dataHQ[i].name
          store.state.transactiondatas[store.state.INDEX][
            y
          ].transaction.open_order.executed_no = store.state.dataHQ[i].new_price
          Vue.set(
            store.state.transactiondatas[store.state.INDEX],
            y,
            store.state.transactiondatas[store.state.INDEX][y]
          )
        }
      }
    }
    // }
  }
  ws.onclose = function(e) {
    console.info(e)
    console.log('closed')
  }
  ws.onerror = function(e) {
    console.info(e)
    console.log('发生异常')
  }
}
